package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.UserLogins;
import com.example.mall.dto.UserLoginsDto;
import com.example.mall.mapper.UserLoginsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.UserLoginsQuery;
import com.example.mall.service.IUserLoginsService;
import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户登录信息表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-14
 */
@Service
public class UserLoginsServiceImpl extends ServiceImpl<UserLoginsMapper, UserLogins> implements IUserLoginsService {

    @Autowired
    private UserLoginsMapper userLoginsMapper;

    @Override
    public PageDTO<UserLoginsDto> queryPage(UserLoginsQuery userLoginsQuery) {
        if (userLoginsQuery == null) {
            userLoginsQuery = new UserLoginsQuery();
        }
        Page<UserLogins> page = userLoginsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<UserLogins> cartPage = lambdaQuery()
                .like(userLoginsQuery.getLoginId()!= null, UserLogins::getLoginId, userLoginsQuery.getLoginId())
                .like(userLoginsQuery.getUserId()!= null, UserLogins::getUserId, userLoginsQuery.getUserId())
                .apply(userLoginsQuery.getLoginTime() != null,
                        "DATE_FORMAT(login_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        userLoginsQuery.getLoginTime())
                .like(userLoginsQuery.getLoginIp()!= null, UserLogins::getLoginIp, userLoginsQuery.getLoginIp())
                .like(userLoginsQuery.getLoginStatus()!= null, UserLogins::getLoginStatus, userLoginsQuery.getLoginStatus())
                .orderByDesc(UserLogins::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, UserLoginsDto.class);
    }

    @Override
    public UserLogins queryUserLoginsById(Long loginId) {
        return userLoginsMapper.selectById(loginId);
    }

    @Override
    public List<UserLogins> queryUserLoginsList() {
        return this.list();
    }

    @Override
    public void insert(UserLoginsDto userLoginsDto) {
        UserLogins userLogins = new UserLogins();
        CopyUtils.copyProperties(userLoginsDto,userLogins);
        userLogins.setLoginId(CodeGenerateUtils.generateUniqueId());
        userLogins.setCreateDate(new Date());
        userLogins.setUpdateDate(new Date());
        userLoginsMapper.insert(userLogins);
    }

    @Override
    public void delete(Long loginId) {
        AssertUtil.isNotNull(loginId,"登录id不能为空");
        UserLogins userLogins = userLoginsMapper.selectById(loginId);
        AssertUtil.isNotNull(userLogins,"登录信息不存在");
        userLogins.setLoginStatus(BaseConstants.LOSE_STATUS);
        userLogins.setUpdateDate(new Date());
        userLoginsMapper.updateById(userLogins);
    }

    @Override
    public void update(UserLoginsDto userLoginsDto) {
        AssertUtil.isNotNull(userLoginsDto.getLoginId(),"登录id不能为空");
        UserLogins userLogins = userLoginsMapper.selectById(userLoginsDto.getLoginId());
        AssertUtil.isNotNull(userLogins,"登录信息不存在");
        CopyUtils.copyProperties(userLoginsDto,userLogins);
        userLogins.setUpdateDate(new Date());
        userLoginsMapper.updateById(userLogins);
    }

    @Override
    public void deleteBatch(List<Long> loginIds) {
        AssertUtil.isNotNull(loginIds,"登录id不能为空");
        List<UserLogins> userLogins = userLoginsMapper.selectBatchIds(loginIds);
        AssertUtil.isNotNull(userLogins,"登录信息不存在");
        for (UserLogins userLogin : userLogins) {
            userLogin.setLoginStatus(BaseConstants.LOSE_STATUS);
            userLogin.setUpdateDate(new Date());
            userLoginsMapper.updateById(userLogin);
        }
    }
}
